package com.meirengu.channel.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.meirengu.channel.service.WithMeService;
import com.meirengu.utils.HttpUtil;
import com.meirengu.utils.MD5Util;
import com.meirengu.webview.common.Constants;
import com.meirengu.webview.model.KwXxjrPush;
import com.meirengu.webview.model.KwXxjrPushLog;
import com.meirengu.webview.service.KwXxjrPushLogService;
import com.meirengu.webview.service.KwXxjrPushService;
import com.meirengu.webview.utils.ConfigUtil;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 贷上我
 *
 * @author Mao
 * @create 2017-12-15 12:11
 */
@Service
public class WithMeServiceImpl implements WithMeService {

    Logger logger = LoggerFactory.getLogger(WithMeServiceImpl.class);
    @Autowired
    private KwXxjrPushService kwXxjrPushService;
    @Autowired
    private KwXxjrPushLogService kwXxjrPushLogService;

    /**
     * @param
     * @return
     */

    public Integer applyLoan(String telephone, String applyName, String sex, String birthdayStr,
                             String loanAmount, String cityName, String workType, String socialType,
                             String fundType, String houseType, String carType, String creditType,
                             String income, String wagesType, String insurType, String applyIp,
                             String Age, String haveWeiLi) throws Exception {

        String url = ConfigUtil.getConfig("withme.url");
        String key = ConfigUtil.getConfig("withme.channel_id");

        Map<String, String> params = new HashMap<>();

        String time = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        params.put("time", time);
        params.put("sign", MD5Util.get32MD5(telephone + "&" + time + key));
        params.put("telephone", telephone);
        params.put("applyName", applyName);
        params.put("sex", sex);
        params.put("birthday", birthdayStr);
        params.put("loanAmount", loanAmount);
        params.put("cityName", cityName);
        params.put("workType", workType);
        params.put("socialType", socialType);
        params.put("fundType", fundType);
        params.put("houseType", houseType);
        params.put("carType", carType);
        params.put("creditType", creditType);
        params.put("income", income);
        params.put("wagesType", wagesType);
        params.put("insurType", insurType);
        params.put("applyIp", applyIp);
        params.put("Age", Age);
        params.put("haveWeiLi", haveWeiLi);

        //接口调用信息表
        KwXxjrPush kwXxjrPush = new KwXxjrPush();
        kwXxjrPush.setInterfaceCode("xxjr");
        kwXxjrPush.setInterfaceName("贷上我");
        kwXxjrPush.setMobile(telephone);
        kwXxjrPush.setCode(Constants.BUSINESS_STATUS_FAIL);


        KwXxjrPushLog kwXxjrPushLog = new KwXxjrPushLog();
        kwXxjrPushLog.setInterfaceCode("xxjr");
        kwXxjrPushLog.setInterfaceName("贷上我");
        kwXxjrPushLog.setMobile(telephone);
        kwXxjrPushLog.setSendUrl(url.toString());
        kwXxjrPushLog.setParams(params.toString());


        HttpUtil.HttpResult hr = null;
        try {
            hr = HttpUtil.doPostForm(url, params);
            int code = hr.getStatusCode();
            if (code == 200) {
                kwXxjrPushLog.setBackUrl(hr.toString());
                kwXxjrPushLog.setHttpCode(200);
                kwXxjrPushLog.setHttpContent(hr.toString());

                logger.info("==WithMeServiceImpl.applyLoan request success. result:{}", hr.toString());
                String content = hr.getContent();
                JSONObject returnMsg = JSONObject.parseObject(content);
                String rCode = returnMsg.getString("returnCode");
                String rMsg = returnMsg.getString("errorMessage");
                if ("000".equals(rCode)) {
                    kwXxjrPush.setCode(Constants.BUSINESS_STATUS_SUCCESS);
                    kwXxjrPush.setMessage(rMsg);
                    logger.info("==WithMeServiceImpl.applyLoan business success");
                } else if ("002".equals(rCode)) {
                    kwXxjrPush.setCode(Constants.BUSINESS_STATUS_SIGNATURE_ERROR);
                    kwXxjrPush.setMessage(rMsg);
                    logger.warn("==WithMeServiceImpl.applyLoan business fail");
                } else if ("003".equals(rCode)) {
                    kwXxjrPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                    kwXxjrPush.setMessage(rMsg);
                    logger.warn("==WithMeServiceImpl.applyLoan business fail");
                } else if ("009".equals(rCode)) {
                    kwXxjrPush.setCode(Constants.BUSINESS_STATUS_OUT_LIMIT);
                    kwXxjrPush.setMessage(rMsg);
                    logger.warn("==WithMeServiceImpl.applyLoan business fail");
                } else {
                    kwXxjrPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                    kwXxjrPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    logger.warn("==WithMeServiceImpl.applyLoan business fail");
                }
            } else {
                kwXxjrPushLog.setBackUrl(hr.toString());
                kwXxjrPushLog.setHttpCode(code);
                kwXxjrPushLog.setHttpContent(hr.toString());
                logger.warn("==WithMeServiceImpl.applyLoan request fail. code:{}, result:{}", code, hr.toString());
            }
        } catch (Exception e) {
            kwXxjrPushLog.setHttpCode(408);
            kwXxjrPushLog.setHttpContent("Exception");
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        } finally {
            kwXxjrPushService.insert(kwXxjrPush);
            kwXxjrPushLog.setPushId(kwXxjrPush.getId());
            kwXxjrPushLogService.insert(kwXxjrPushLog);
            return kwXxjrPush.getCode();
        }

    }

    /**
     * 贷上我2
     *
     * @param
     * @return
     */

    public Integer applyLoan2(String telephone, String applyName, String sex, String birthdayStr,
                              String loanAmount, String cityName, String workType, String socialType,
                              String fundType, String houseType, String carType, String creditType,
                              String income, String wagesType, String insurType, String applyIp,
                              String Age, String haveWeiLi) throws Exception {

        String url = ConfigUtil.getConfig("withme2.url");
        String key = ConfigUtil.getConfig("withme2.channel_id");

        Map<String, String> params = new HashMap<>();

        String time = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        params.put("time", time);
        params.put("sign", MD5Util.get32MD5(telephone + "&" + time + key));
        params.put("telephone", telephone);
        params.put("applyName", applyName);
        params.put("sex", sex);
        params.put("birthday", birthdayStr);
        params.put("loanAmount", loanAmount);
        params.put("cityName", cityName);
        params.put("workType", workType);
        params.put("socialType", socialType);
        params.put("fundType", fundType);
        params.put("houseType", houseType);
        params.put("carType", carType);
        params.put("creditType", creditType);
        params.put("income", income);
        params.put("wagesType", wagesType);
        params.put("insurType", insurType);
        params.put("applyIp", applyIp);
        params.put("Age", Age);
        params.put("haveWeiLi", haveWeiLi);


        //接口调用信息表
        KwXxjrPush kwXxjrPush = new KwXxjrPush();
        kwXxjrPush.setInterfaceCode("xxjr2");
        kwXxjrPush.setInterfaceName("贷上我2");
        kwXxjrPush.setMobile(telephone);
        kwXxjrPush.setCode(Constants.BUSINESS_STATUS_FAIL);


        KwXxjrPushLog kwXxjrPushLog = new KwXxjrPushLog();
        kwXxjrPushLog.setInterfaceCode("xxjr2");
        kwXxjrPushLog.setInterfaceName("贷上我2");
        kwXxjrPushLog.setMobile(telephone);
        kwXxjrPushLog.setSendUrl(url.toString());
        kwXxjrPushLog.setParams(params.toString());


        HttpUtil.HttpResult hr = null;
        try {
            hr = HttpUtil.doPostForm(url, params);
            int code = hr.getStatusCode();
            if (code == 200) {
                kwXxjrPushLog.setBackUrl(hr.toString());
                kwXxjrPushLog.setHttpCode(200);
                kwXxjrPushLog.setHttpContent(hr.toString());

                logger.info("==WithMeServiceImpl.applyLoan2 request success. result:{}", hr.toString());
                String content = hr.getContent();
                JSONObject returnMsg = JSONObject.parseObject(content);
                String rCode = returnMsg.getString("returnCode");
                String rMsg = returnMsg.getString("errorMessage");
                if ("000".equals(rCode)) {
                    kwXxjrPush.setCode(Constants.BUSINESS_STATUS_SUCCESS);
                    kwXxjrPush.setMessage(rMsg);
                    logger.info("==WithMeServiceImpl.applyLoan2 business success");
                } else if ("002".equals(rCode)) {
                    kwXxjrPush.setCode(Constants.BUSINESS_STATUS_SIGNATURE_ERROR);
                    kwXxjrPush.setMessage(rMsg);
                    logger.warn("==WithMeServiceImpl.applyLoan2 business fail");
                } else if ("003".equals(rCode)) {
                    kwXxjrPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                    kwXxjrPush.setMessage(rMsg);
                    logger.warn("==WithMeServiceImpl.applyLoan2 business fail");
                } else if ("009".equals(rCode)) {
                    kwXxjrPush.setCode(Constants.BUSINESS_STATUS_OUT_LIMIT);
                    kwXxjrPush.setMessage(rMsg);
                    logger.warn("==WithMeServiceImpl.applyLoan2 business fail");
                } else {
                    kwXxjrPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                    kwXxjrPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    logger.warn("==WithMeServiceImpl.applyLoan2 business fail");
                }
            } else {
                kwXxjrPushLog.setBackUrl(hr.toString());
                kwXxjrPushLog.setHttpCode(code);
                kwXxjrPushLog.setHttpContent(hr.toString());
                logger.warn("==WithMeService2Impl.applyLoan request fail. code:{}, result:{}", code, hr.toString());
            }
        } catch (Exception e) {
            kwXxjrPushLog.setHttpCode(408);
            kwXxjrPushLog.setHttpContent("Exception");
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        } finally {
            kwXxjrPushService.insert(kwXxjrPush);
            kwXxjrPushLog.setPushId(kwXxjrPush.getId());
            kwXxjrPushLogService.insert(kwXxjrPushLog);
            return kwXxjrPush.getCode();
        }

    }

}
